<?php
/**
 * Created by PhpStorm.
 * User: wangxj
 * Date: 2017/06/21
 */

namespace App\Helper;

class DateHelper
{
    public static function now()
    {
        return date('Y-m-d H:i:s', time());
    }

    public static function day()
    {
        return date('Y-m-d', time());
    }

    public static function timeAPM()
    {
        //计算时间
        $houre       = date('H');
        $currentDate = date('Y年m月d日');

        if($houre<11)
        {
            $currentDate .= '上午';
        }
        else
        {
            $currentDate .= '下午';
        }

        return $currentDate;
    }
    //取得年龄
    public static function getAge($birthday)
    {
        if(!isset($birthday) || empty($birthday) || $birthday == '1000-01-01' || $birthday == '0000-00-00')
        {
            return '未填写';
        }

        $datetime_start = new \DateTime($birthday);
        $datetime_end = new \DateTime(today());
        $dateInterval = $datetime_start->diff($datetime_end);

        $y = $dateInterval->y;
        $m = $dateInterval->m;
        $d = $dateInterval->d;

        if($y > 0)
        {
            if($y<3 && $m > 0)
            {
                return $y . '岁' . $m . '个月';
            }
            return $y . '岁';
        }
        else if($m > 0)
        {
            return $m . '个月';
        }
        else if($d > 0)
        {
            return $d . '天';
        }
        else
        {
            return '0岁';
        }
    }

    // 获取时间差，返回天数
    public static function getDiffDate($time)
    {
        $current_time = strtotime (date("Y-m-d H:i:s"));
        $old_time = strtotime ($time);
        return ($current_time-$old_time)/86400; //60s*60min*24h
    }

    // 获取时间差，返回小时数
    public static function getDiffHour($beginTime, $endTime)
    {
        $begin_time = strtotime ($beginTime);
        $end_time = strtotime ($endTime);
        return ($end_time - $begin_time)/(60*60); //60s*60min*24h
    }

    // 获取当月第一天和最后一天
    public static function getCurrentMonth()
    {
        $date = date("Y-m-d H:i:s");
        $firstday = date('Y-m-01 00:00:00', strtotime($date));
        $lastday = date('Y-m-d 23:59:59', strtotime("$firstday +1 month -1 day"));
        return [$firstday, $lastday];
    }

    //取得本周周一日期
    public static function getWeekFirstDate()
    {
        return date('Y-m-d 00:00:00', strtotime('this week'));
    }

    //取得本周周日日期
    public static function getWeekLastDate()
    {
        return date('Y-m-d 23:59:59', strtotime('last day next week'));
    }

    //取得出诊、考勤等本月日期范围（现在日期<=25：从上个月25到现在; 现在日期>25: 从本月26到现在日期————因为只有日期，所以两个日期可以相等）
    public static function getRangeDate()
    {
        $startDate = '';
        $nowTime = time();
        $endDate = date('Y-m-d', $nowTime);
        $toDay = date('d', $nowTime);
        if ($toDay <= 25) {  // 25号之前
            $startMonthTime = $nowTime - 3600*24*26;    // *26 可以保证是上个月，又避免2月特殊情况
            $startDate = date('Y-m',$startMonthTime);
            $startDate = $startDate . '-26';
        }
        else
        {
            $startDate = date('Y-m',$nowTime);
            $startDate = $startDate . '-26';
        }
        return array(0=>$startDate, 1=>$endDate);
    }

    // 获取上周星期日的日期
    public function lastSunday()
    {
        // return date('Y年m月d日', strtotime('last Sunday'));    // 显示字数太多，导致显示错位
        return date('m月d日', strtotime('last Sunday'));
    }

    // 获取昨天的日期
    public function yesterday()
    {
        // return date('Y年m月d日', strtotime('-1 day')); // 显示字数太多，导致显示错位
        return date('m月d日', strtotime('-1 day'));
    }

    // 获取考勤月开始日期和结束日期（用于显示）
    public static function getMonthDate()
    {
        $startDate = '';
        $nowTime = time();
        $toDay = date('d', $nowTime);
        if ($toDay <= 25) {  // 25号之前
            $startMonthTime = $nowTime - 3600*24*26;    // *26 可以保证是上个月，又避免2月特殊情况
            $startDate = date('m月',$startMonthTime);
            $startDate = $startDate . '26日';
            $endDate   = date('m月',$nowTime);
            $endDate   = $endDate . '25日';
        }
        else
        {
            $startDate = date('m月',$nowTime);
            $startDate = $startDate . '26日';
            $endDateTiem = $nowTime + 3600*24*7;
            $endDate = date('m月',$endDateTiem);
            $endDate   = $endDate . '25日';
        }
        return array(0=>$startDate, 1=>$endDate);
    }

    // 根据开始日期和结束日期生成期间的星期数组
    public static function getWeeksList($startDate='', $endDate='')
    {
        // 开始日期和结束日期处理
        if (($startDate == '') || ($startDate < '2018-06-11'))
        {
            $startDate = '2018-06-11';
        }
        $startWeekDay = date('w', strtotime($startDate));
        if ($startWeekDay != 1)
        {
            $startDate = date('Y-m-d', strtotime('last Monday', strtotime($startDate)));
        }

        $maxYear = date('Y',time()) + 1;
        $maxDate = $maxYear . '-12-31';
        if (($endDate == '') || ($endDate > $maxDate))
        {
            $endDate = date('Y-m-d',strtotime('+4 week'));
        }
        $endWeekDay = date('w', strtotime($endDate));
        if ($endWeekDay != 0)
        {
            $endDate = date('Y-m-d', strtotime('next Sunday', strtotime($endDate)));
        }

        // 生成星期数组
        $weeksList = [];
        if ($startDate < $endDate)
        {
            $finalWeekStartDate = date('Y-m-d', strtotime('last Monday', strtotime($endDate)));
            while ($startDate <= $finalWeekStartDate) {
                $weekData                   = [];
                $weekData['start']['date']  = $startDate;
                $weekData['start']['text']  = date('Y年m月d日',strtotime($startDate));
                $weekData['end']['date']    = date('Y-m-d', strtotime('+6 day', strtotime($startDate)));
                $weekData['end']['text']    = date('m月d日',strtotime($weekData['end']['date']));
                // $weeksList[]                = $weekData;    // 正序
                array_unshift($weeksList, $weekData);   // 倒序
                $startDate                  = date('Y-m-d', strtotime('next Monday', strtotime($startDate)));
            }
        }
        return $weeksList;
    }

    //获取工作时间
    public function getWorkTime($time)
    {
        switch ($time) {
            case 1:
                $text = '上午';
                break;
            case 2:
                $text = '下午';
                break;
            case 3:
                $text = '全天';
                break;
            default:
                $text = '';
                break;
        }
        return $text;
    }

    // 获取两个时间差年月日
    public static function timediff($begin_time,$end_time)
    {
        $begin_time = strtotime($begin_time);
        $end_time = strtotime($end_time);
        if($begin_time < $end_time){
            $starttime = $begin_time;
            $endtime = $end_time;
        }else{
            $starttime = $end_time;
            $endtime = $begin_time;
        }
        //计算天数
        $timediff = $endtime-$starttime;
        $days = intval($timediff/86400);
        //计算小时数
        $remain = $timediff%86400;
        $hours = intval($remain/3600);
        //计算分钟数
        $remain = $remain%3600;
        $mins = intval($remain/60);
        //计算秒数
        $secs = $remain%60;
        $res = array("day" => $days,"hour" => $hours,"min" => $mins,"sec" => $secs);
        return $res;
    }

    /*
    *function：计算两个日期相隔多少年，多少月，多少天
    *param string $date1[格式如：2011-11-5]
    *param string $date2[格式如：2012-12-01]
    *return array array('年','月','日');
    */
    public static function diffDate($date1, $date2)
    {
        if (strtotime($date1) > strtotime($date2))
        {
            $tmp   = $date2;
            $date2 = $date1;
            $date1 = $tmp;
        }
        list($Y1, $m1, $d1) = explode('-', $date1);
        list($Y2, $m2, $d2) = explode('-', $date2);
        $Y = $Y2 - $Y1;
        $m = $m2 - $m1;
        $d = $d2 - $d1;
        if ($d < 0)
        {
            $d += (int)date('t', strtotime("-1 month $date2"));
            $m--;
        }
        if ($m < 0)
        {
            $m += 12;
            $Y--;
        }
        return array('year'  => $Y,
            'month' => $m,
            'day'   => $d
        );
    }

    // 获取当前周几，1周一2周二...7周日，0是默认无，周日是7
    public static function getCurrentWeek()
    {
        $duty = date("w"); //这个可以获取数字星期比如123，注意0是星期日
        $duty = $duty == 0 ? 7 : (int)$duty;
        return $duty;
    }
}